package tw.org.iii.dataaccesssample;

import tw.org.iii.dataaccesssample.data.CDbManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ActSqlite extends Activity {


	private long lngCurrentPK=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actsqlite);
		InitialComponent();
	}
	
	OnClickListener btnNew_click=new OnClickListener(){
		public void onClick(View arg0) {
			
		ContentValues data =new ContentValues();
		data.put("fId", txtId.getText().toString());
		data.put("fSubject", txtSubject.getText().toString());
		data.put("fDate", txtDate.getText().toString());
		data.put("fTime", txtTime.getText().toString());
		data.put("fLocation", txtLocation.getText().toString());
		
		CDbManager db=new CDbManager(ActSqlite.this);
		db.newData("tMeeting", data);
		
		Toast.makeText(ActSqlite.this, "新增資料成功", Toast.LENGTH_SHORT).show();
			
	}};
	OnClickListener btnDelete_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			CDbManager db=new CDbManager(ActSqlite.this);
			db.DeleteData("tMeeting", lngCurrentPK);
			Toast.makeText(ActSqlite.this, "資料已經被刪除", Toast.LENGTH_SHORT).show();
			
	}};	
	OnClickListener btnEdit_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			ContentValues data =new ContentValues();
			data.put("fId", txtId.getText().toString());
			data.put("fSubject", txtSubject.getText().toString());
			data.put("fDate", txtDate.getText().toString());
			data.put("fTime", txtTime.getText().toString());
			data.put("fLocation", txtLocation.getText().toString());
			
			CDbManager db=new CDbManager(ActSqlite.this);
			db.UpdateData("tMeeting", data,lngCurrentPK);
			
			Toast.makeText(ActSqlite.this, "修改資料成功", Toast.LENGTH_SHORT).show();

			
	}};
	OnClickListener btnBrowse_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			Intent intent =new Intent(ActSqlite.this,ActList.class);
			startActivityForResult(intent,122);
			
	}};	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(data==null)
			return;
		if(data.getExtras()==null)
			return;
		lngCurrentPK=data.getExtras().getLong("KK");
		String strPK=String.valueOf(lngCurrentPK);
		CDbManager db=new CDbManager(this);
		Cursor table=db.GetBySql("SELECT * FROM tMeeting WHERE _id="+strPK);
		if(table.getCount()>0){
			table.moveToFirst();
			txtId.setText(table.getString(1));
			txtSubject.setText(table.getString(2));
			txtDate.setText(table.getString(3));
			txtTime.setText(table.getString(4));
			txtLocation.setText(table.getString(5));
		}
	}
	private void InitialComponent() {

		btnNew=(Button)findViewById(R.id.btnNew);
		btnNew.setOnClickListener(btnNew_click);
		btnDelete=(Button)findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(btnDelete_click);
		btnEdit=(Button)findViewById(R.id.btnEdit);
		btnEdit.setOnClickListener(btnEdit_click);
		txtId=(EditText)findViewById(R.id.txtId);
		btnBrowse=(Button)findViewById(R.id.btnBrowse);
		btnBrowse.setOnClickListener(btnBrowse_click);
		txtSubject=(EditText)findViewById(R.id.txtSubject);
		txtDate=(EditText)findViewById(R.id.txtDate);
		txtTime=(EditText)findViewById(R.id.txtTime);
		txtLocation=(EditText)findViewById(R.id.txtLocation);
	}
   

	Button btnNew=null;
	Button btnDelete=null;
	Button btnEdit=null;
	Button btnBrowse=null;
	
	
	EditText txtId=null;
	EditText txtSubject=null;
	EditText txtDate=null;
	EditText txtTime=null;
	EditText txtLocation=null;
	
	
	

}
